# -*- coding: utf-8 -*-

from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator
from datetime import timedelta

from jms.dwd.oms.dwd_oms_order_dt import jms_dwd__dwd_oms_order_dt
from jms.dwd.tab.dwd_tab_barscan_taking_base_dt import jms_dwd__dwd_tab_barscan_taking_base_dt
from jms.dwd.tab.dwd_tab_barscan_send_base_dt import jms_dwd__dwd_tab_barscan_send_base_dt
from jms.dwd.tab.dwd_tab_barscan_arrival_base_dt import jms_dwd__dwd_tab_barscan_arrival_base_dt
from jms.dwd.tab.dwd_tab_barscan_deliver_base_dt import jms_dwd__dwd_tab_barscan_deliver_base_dt
from jms.dwd.tab.dwd_tab_barscan_sign_base_dt import jms_dwd__dwd_tab_barscan_sign_base_dt
from jms.dwd.tab.dwd_tab_barscan_collect_base_dt import jms_dwd__dwd_tab_barscan_collect_base_dt
from jms.dim.dim_network_whole_massage import jms_dim__dim_network_whole_massage
from jms.time_sensor.time_after_04_45 import time_after_04_45

jms_dm__dm_barscan_summary_byuser_dt = SparkSqlOperator(
    task_id='jms_dm__dm_barscan_summary_byuser_dt',
    task_concurrency=1,
    pool_slots=2,
    master='yarn',
    name='jms_dm__dm_barscan_summary_byuser_dt_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dm/dm_barscan_summary_byuser_dt/execute.sql',
    driver_cores=4 , 
    driver_memory='4G' , 
    executor_cores=4 , 
    executor_memory='4G' , 
    num_executors=12 , 
    conf={'spark.dynamicAllocation.enabled'                  : 'true',  # 动态资源开启
          'spark.shuffle.service.enabled'                    : 'true',  # 动态资源 Shuffle 服务开启
        'spark.dynamicAllocation.maxExecutors'             : 14 , 
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 120,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode'         : 'dynamic',  # 允许删改已存在的分区
        'spark.executor.memoryOverhead'             : '2G' , 
          'spark.sql.shuffle.partitions'                     : 800,
          'spark.default.paralleism'                         : 800,
          },
    hiveconf={'hive.exec.dynamic.partition'             : 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode'        : 'nonstrict',
              'hive.exec.max.dynamic.partitions'        : 20,  # 每天生成 20 个分区
              'hive.exec.max.dynamic.partitions.pernode': 20,  # 每天生成 20 个分区
              },
    execution_timeout=timedelta(minutes=75),
    email=['jarl.huang@yl-scm.com','yl_bigdata@yl-scm.com'],
)

jms_dm__dm_barscan_summary_byuser_dt << [
    jms_dwd__dwd_oms_order_dt,
    jms_dwd__dwd_tab_barscan_taking_base_dt,
    jms_dwd__dwd_tab_barscan_send_base_dt,
    jms_dwd__dwd_tab_barscan_arrival_base_dt,
    jms_dwd__dwd_tab_barscan_deliver_base_dt,
    jms_dwd__dwd_tab_barscan_sign_base_dt,
    jms_dwd__dwd_tab_barscan_collect_base_dt,
    jms_dim__dim_network_whole_massage,
    time_after_04_45
]
